/*
	Font Manager, this file is part of the Gallion Fight Game Project
	all rights reserved, Copyright (2010) Brunie Nicolas
*/
#include <GL/glew.h>
#include "SFML/Graphics.hpp"
#include <ft2build.h>
#include FT_FREETYPE_H

#include <string>
#include <vector>
#include <sstream>
#include <iostream>



#ifndef __FONT_MANAGER_HPP__
#define __FONT_MANAGER_HPP__

using namespace std;

class FontExporter {
	private:
		/** associated FreeType library */
		FT_Library m_library;

		/** associated FreeType face */
		FT_Face m_face;
		int m_size;

		/** texture data for font */
		unsigned int texID_;

		/** drawing list openGL id */
		unsigned int listBase_;

		/** width of each char */
		std::vector<unsigned char> widths_;

		/** uniform character height */
		unsigned char height_;

		/** stream drawing stuff */
		std::ostringstream ss_;

		/** DEPRECATED : temporary X position */
		float drawX_;
		
		/** DEPRECATED : temporary Y position */
		float drawY_;

		/** constant SPACE position in char array */
		static const unsigned int SPACE = 32;

		/** constant number of characters */
		static const unsigned int NUM_CHARS = 96;
	public:
		/** creating FontExporter from font file (ttf...)*/
		FontExporter(string filename);

		/** rendering a char string on the current framebugger,
		  * starting at (x,y), with the font associated to this
		  * FontExporter */
		float drawString(float x, float y, string str, float scale_ = 1.0f);

		/**	rendering a string, with a limited width of width_, 
		 * 	\n are inserted 
		 *	return 	height of the results
		 *	NOT IMPLEMENTED YET */
		float drawBoundedString(float x, float y, string str, float length_ = -1.0f, float scale_ = 1.0f);

		/** get the string indexes that last fit into width_ */
		int getLimitIndex(string str_, float length_);

		/** return the length of the string given as argument */
		float getStringLength(string str);

		/** return the substring of str_ that does not overpass length in size*/
		string getLimitedString(string str_, float length);
};


class FontManager {
	protected:
		/** map for storing yet created FontExporter, according
		  * to the name of the file font they come from */
		static map<string, FontExporter*> m_fontMap;

	public:
		/** function for retrieving a FontExporter for a certain
		  * font file. If a fontExporter has yet been used for this font
		  * this function return this fontExporter, else it creates a new one
		  * exporting font from given file name and return a pointer to it*/
		static FontExporter* getFontExporter(string filename);
};


#endif
